package com.ele.client.activity.terminal;

import com.ele.client.ClientFactory;
import com.ele.client.activity.auth.LoginPlace;
import com.ele.client.activity.terminal.widget.ImagesWidget;
import com.ele.client.activity.terminal.widget.WordWidget;
import com.ele.client.activity.terminal.widget.WordWidgetImpl;
import com.ele.client.rpc.SimpleAsyncCallback;
import com.ele.client.util.UserHelper;
import com.ele.model.Question;
import com.ele.model.Word;
import com.ele.shared.action.*;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;

import java.util.ArrayList;
import java.util.List;

public class TerminalActivity extends AbstractActivity implements TerminalView.Presenter {

    private ClientFactory clientFactory;
    private TerminalView view;
    private TerminalPlace place;

    private Question question;

    public TerminalActivity(TerminalPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        this.place = place;
    }

    @Override
    public void start(AcceptsOneWidget container, EventBus eventBus) {
        if (UserHelper.getUser() == null) {
            UserHelper.setReferrer(place);
            clientFactory.getPlaceController().goTo(new LoginPlace(null));
        } else {
            view = clientFactory.getTerminalView();
            view.setPresenter(this);
            container.setWidget(view.asWidget());

            load(null);
        }
    }

    private void load(String answer) {
        clientFactory.getApplicationService().getActionsList(answer, new SimpleAsyncCallback<List<Action>>() {
            @Override
            public void onSuccess(List<Action> result) {
                if (result != null) {
                    for (Action action : result) {
                        if (action instanceof SetWordsAction) {
                            setWords(((SetWordsAction) action).getValue());
                        } else if (action instanceof SetDescriptionAction) {
                            setDescription(((SetDescriptionAction) action).getValue());
                        } else if (action instanceof AppendDescriptionAction) {
                            appendDescription(((AppendDescriptionAction) action).getValue());
                        } else if (action instanceof SetQuestionAction) {
                            setQuestion(((SetQuestionAction) action).getValue());
                        } else if (action instanceof AddQuestionAction) {
                            addQuestion(((AddQuestionAction) action).getValue());
                        } else if (action instanceof LoadImagesAction) {
                            loadImages(((LoadImagesAction) action).getValue());
                        } else if (action instanceof SetAnswerErrorAction) {
                            setAnswerError(((SetAnswerErrorAction) action).getValue());
                        } else if (action instanceof SetAnswerSuccessAction) {
                            setAnswerSuccess(((SetAnswerSuccessAction) action).getValue());
                        }
                    }
                }
            }
        });
    }

    private void setAnswerSuccess(Void value) {
        view.setAnswerSuccess();
    }

    private void setAnswerError(List<String> validAnswers) {
        view.setAnswerExamples(validAnswers);
    }

    private void loadImages(String keyword) {
        ImagesWidget widget = view.getImagesWidget();
        boolean valid = keyword != null && !keyword.isEmpty();
        if (valid) {
            widget.load(keyword);
        }
        widget.setVisible(valid);
    }

    private void addQuestion(Question question) {
        view.setQuestionText(question.getText());
    }

    private void setQuestion(Question question) {
        this.question = question;
        view.setQuestionText(question.getText());
        view.clearAnswer();
//        QuestionWidget widget = new QuestionWidgetImpl(question.getId(), question.getText());
//        view.setQuestionWidget(widget);
    }

    private void appendDescription(String description) {
        view.appendDescriptionText(description);
    }

    private void setDescription(String description) {
        view.setDescriptionText(description);
    }

    private void setWords(List<Word> words) {
        List<IsWidget> widgets = new ArrayList<IsWidget>();

        for (Word word : words) {
            WordWidget widget = new WordWidgetImpl();
            widget.setText(word.getText());
            widget.setTranslation(word.getTranslation());
            widgets.add(widget);
        }

        view.setWordWidgets(widgets);
    }

    @Override
    public void checkAnswer(String answer) {
        load(answer);
    }
}
